package com.dick.base.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BusinessSpecificationMapper;
import com.dick.base.model.BusinessSpecification;
import com.dick.base.parameter.SpecificationParameter;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 规格服务
 */
@Service
public class SpecificationService {

    @Autowired
    private BusinessSpecificationMapper businessSpecificationMapper;

    /**
     * 规格分页查询
     * @param specification
     * @param pageInfo
     * @return
     */
    @SmartQuery(clazz = BusinessSpecification.class, likeFields = {"name", "material"})
    public Page<BusinessSpecification> specificationPage(BusinessSpecification specification, PageInfo pageInfo) {
        return businessSpecificationMapper.selectPage(SmartQueryUtil.getPage(), SmartQueryUtil.getQueryWrapper());
    }

    /**
     * 规格详情
     * @param id
     * @return
     */
    public BusinessSpecification businessSpecificationDetail(Long id) {
        return businessSpecificationMapper.selectById(id);
    }

    /**
     * 添加规格
     * @param specificationParameter
     */
    public void addSpecification(SpecificationParameter specificationParameter) {
        BusinessSpecification businessSpecification = new BusinessSpecification();
        businessSpecification.setDeleted(false);
        businessSpecification.setCreateTime(DateUtil.now());
        businessSpecification.setMaterial(specificationParameter.getMaterial());
        businessSpecification.setName(specificationParameter.getName());
        businessSpecification.setThickness(specificationParameter.getThickness());
        businessSpecification.setWidth(specificationParameter.getWidth());
        businessSpecification.setId(IdUtil.nextId());
        businessSpecificationMapper.insert(businessSpecification);
    }

    /**
     * 弃用规格
     * @param id
     */
    public void removeSpecification(Long id) {
        businessSpecificationMapper.deleteById(id);
    }
}
